package cxm.proj.xtalk.smack;

import cxm.lib.util.MyLog;

public class ConnectManager {
	private static ConnectManager instance = null;
	private String serverIp = null;
	private String serviceName = null;
	private Connector connector = null;
	
	private ConnectManager() {
	}
	
	public void createConnector(String serverip, String serverName) {
		this.serverIp = serverip;
		this.serviceName = serverName;
		this.connector = new Connector(this.serverIp, this.serviceName);
	}
	
	public boolean hasConnector() {
		return null != this.connector;
	}
	
	public Connector getConnector() {
		if (null == this.connector)
			throw new IllegalStateException("Connector unset");
		return this.connector;
	}
	
	public String getServiceName() {
		return this.serviceName;
	}
	
	public String getServerIp() {
		return this.serverIp;
	}
	
	public static void Release() {
		if (null == instance)
			return;

		if (null != instance.connector) {
			try {
				instance.connector.disconnect();
			} catch (Exception e) {
				MyLog.Log(MyLog.Level.Error, "Disconnect error when Release: " + e);
			}
		}
		instance.serverIp = null;
		instance.serviceName = null;
		instance = null;
	}
	
	public static ConnectManager getInstance() {
		if (null == instance)
			instance = new ConnectManager();
		
		return instance;
	}
}
